package com.power.using.sharlockchat.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.power.using.sharlockchat.R;
import com.power.using.sharlockchat.adapter.SloutedRecyclerAdapter;
import com.power.using.sharlockchat.adapter.SmartViewHolder;
import com.power.using.sharlockchat.application.MyApplication;
import com.power.using.sharlockchat.base.BaseMainFragment;
import com.power.using.sharlockchat.bean.ShowBean;
import com.power.using.sharlockchat.eventbus.StartNewFragmentEvent;
import com.power.using.sharlockchat.view.GridSpacingItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.power.using.sharlockchat.utils.loading.LoadingDialog.cancelLoadingDialog;


/**
 * Created by 聪明一只哈 on 2018/5/25.
 * Time: 2018/5/25  10:35
 */

public class SolvedFrament extends BaseMainFragment {


    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.home_rv)
    RecyclerView homeRv;
    @BindView(R.id.refreshAlreadayLayout)
    SmartRefreshLayout refreshAlreadayLayout;
    Unbinder unbinder;
    private SloutedRecyclerAdapter<ShowBean> mAdapter;

    public static SolvedFrament newInstance() {

        Bundle args = new Bundle();
        SolvedFrament fragment = new SolvedFrament();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_two, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView(view);
        return view;
    }

    private void initView(View view) {
        toolbarTitle.setText("已解答列表");
    }

    //懒加载

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initRVData();
        initListener();
    }

    private int i_flag=0;
    private int size_flag=0;
    private Collection<ShowBean> initData() {
//        List<ShowBean> list= new ArrayList<>();
//        if(size_flag==0){
//            size_flag=20;
//            i_flag=0;
//        }else{
//            i_flag=size_flag;
//            size_flag+=20;
//
//        }
//
//        for (int i = i_flag; i < size_flag; i++) {
//            list.add(new ShowBean("标题展示(已解答)" + i, "这个是问题的描述(已解答)","",""));
//        }
        List<ShowBean> showBeanList = MyApplication.getShowBeanList();
        if(showBeanList==null||showBeanList.size()==0){
            return null;
        }
        return showBeanList;
    }

    private void initRVData() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        homeRv.setLayoutManager(layoutManager);
        homeRv.addItemDecoration(new GridSpacingItemDecoration(getResources().getDimensionPixelSize(R.dimen.padding_middle),
                getResources().getDimensionPixelSize(R.dimen.padding_middle), false));

        homeRv.setAdapter(mAdapter = new SloutedRecyclerAdapter<ShowBean>(R.layout.item_question_rv) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, ShowBean model, int position) {
                holder.text(R.id.tv_ques_status,"处理完毕"+model.getPosition());
                holder.text(R.id.tv_item_title,mAdapter.getListShow().get(position).getTitle());
                holder.text(R.id.tv_item_description, mAdapter.getListShow().get(position).getDescription());
                holder.textColorId(R.id.tv_item_description, R.color.blue_400);
            }
        });

        //开启自动加载功能（非必须）
        refreshAlreadayLayout.setEnableAutoLoadMore(true);

        //触发自动刷新
        refreshAlreadayLayout.autoRefresh();

    }

    private void initListener() {

        homeRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            int SCROLL_STATE_IDLE = 0;
            int SCROLL_STATE_TOUCH_SCROLL = 1;
            int SCROLL_STATE_FLING = 2;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == SCROLL_STATE_IDLE) {
                    Log.i("hashiqi", "SCROLL_STATE_IDLE");
                } else if (newState == SCROLL_STATE_TOUCH_SCROLL) {
                    Log.i("hashiqi", "SCROLL_STATE_TOUCH_SCROLL");
                } else if (newState == SCROLL_STATE_FLING) {
                    Log.i("hashiqi", "SCROLL_STATE_FLING");
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });



        //下拉刷新
        refreshAlreadayLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(initData()==null){
                            refreshLayout.finishRefresh();
                            return;
                        }
                        mAdapter.refresh(initData());
                        refreshLayout.finishRefresh();
                        refreshLayout.setNoMoreData(false);
                    }
                }, 2000);
            }
        });

        //上拉加载
        refreshAlreadayLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        if ( mAdapter.getItemCount()>= 60) {
                            Toast.makeText(getActivity(), "数据全部加载完毕", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                        } else {
                            mAdapter.loadMore(initData());
                            refreshLayout.finishLoadMore();
                        }
                    }
                }, 2000);
            }
        });

        mAdapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ShowBean showBean = mAdapter.getListShow().get(position);

                EventBus.getDefault().post(new StartNewFragmentEvent(SolvedDetialFragment.newInstance(showBean)));



            }
        });


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
        OkHttpUtils.getInstance().cancelTag(this);
        cancelLoadingDialog();
    }
}
